﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Maticsoft.DBUtility;
using System.Data.SqlClient;
using V5_Model;

namespace V5_DAL
{
    public class DALArticleClass
    {
        #region  成员方法
        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("ClassID", "K_ArticleClass");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ClassID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from K_ArticleClass");
            strSql.Append(" where ClassID=@ClassID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ClassID", SqlDbType.Int,4)};
            parameters[0].Value = ClassID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(ModelArticleClass model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into K_ArticleClass(");
            strSql.Append("ClassID,ChannelID,ClassName,Child,Depth,OrderID,ParentID,RootID,ParentPath,ReadMe,ClassDescription,ClassKeywords,AddDateTime)");
            strSql.Append(" values (");
            strSql.Append("@ClassID,@ChannelID,@ClassName,@Child,@Depth,@OrderID,@ParentID,@RootID,@ParentPath,@ReadMe,@ClassDescription,@ClassKeywords,@AddDateTime)");
            SqlParameter[] parameters = {
					new SqlParameter("@ClassID", SqlDbType.Int,4),
					new SqlParameter("@ChannelID", SqlDbType.Int,4),
					new SqlParameter("@ClassName", SqlDbType.VarChar,255),
					new SqlParameter("@Child", SqlDbType.Int,4),
					new SqlParameter("@Depth", SqlDbType.Int,4),
					new SqlParameter("@OrderID", SqlDbType.Int,4),
					new SqlParameter("@ParentID", SqlDbType.Int,4),
					new SqlParameter("@RootID", SqlDbType.Int,4),
					new SqlParameter("@ParentPath", SqlDbType.VarChar,255),
					new SqlParameter("@ReadMe", SqlDbType.Text),
					new SqlParameter("@ClassDescription", SqlDbType.VarChar,255),
					new SqlParameter("@ClassKeywords", SqlDbType.VarChar,255),
					new SqlParameter("@AddDateTime", SqlDbType.DateTime)};
            parameters[0].Value = model.ClassID;
            parameters[1].Value = model.ChannelID;
            parameters[2].Value = model.ClassName;
            parameters[3].Value = model.Child;
            parameters[4].Value = model.Depth;
            parameters[5].Value = model.OrderID;
            parameters[6].Value = model.ParentID;
            parameters[7].Value = model.RootID;
            parameters[8].Value = model.ParentPath;
            parameters[9].Value = model.ReadMe;
            parameters[10].Value = model.ClassDescription;
            parameters[11].Value = model.ClassKeywords;
            parameters[12].Value = model.AddDateTime;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(ModelArticleClass model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update K_ArticleClass set ");
            strSql.Append("ChannelID=@ChannelID,");
            strSql.Append("ClassName=@ClassName,");
            strSql.Append("Child=@Child,");
            strSql.Append("Depth=@Depth,");
            strSql.Append("OrderID=@OrderID,");
            strSql.Append("ParentID=@ParentID,");
            strSql.Append("RootID=@RootID,");
            strSql.Append("ParentPath=@ParentPath,");
            strSql.Append("ReadMe=@ReadMe,");
            strSql.Append("ClassDescription=@ClassDescription,");
            strSql.Append("ClassKeywords=@ClassKeywords,");
            strSql.Append("AddDateTime=@AddDateTime");
            strSql.Append(" where ClassID=@ClassID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ClassID", SqlDbType.Int,4),
					new SqlParameter("@ChannelID", SqlDbType.Int,4),
					new SqlParameter("@ClassName", SqlDbType.VarChar,255),
					new SqlParameter("@Child", SqlDbType.Int,4),
					new SqlParameter("@Depth", SqlDbType.Int,4),
					new SqlParameter("@OrderID", SqlDbType.Int,4),
					new SqlParameter("@ParentID", SqlDbType.Int,4),
					new SqlParameter("@RootID", SqlDbType.Int,4),
					new SqlParameter("@ParentPath", SqlDbType.VarChar,255),
					new SqlParameter("@ReadMe", SqlDbType.Text),
					new SqlParameter("@ClassDescription", SqlDbType.VarChar,255),
					new SqlParameter("@ClassKeywords", SqlDbType.VarChar,255),
					new SqlParameter("@AddDateTime", SqlDbType.DateTime)};
            parameters[0].Value = model.ClassID;
            parameters[1].Value = model.ChannelID;
            parameters[2].Value = model.ClassName;
            parameters[3].Value = model.Child;
            parameters[4].Value = model.Depth;
            parameters[5].Value = model.OrderID;
            parameters[6].Value = model.ParentID;
            parameters[7].Value = model.RootID;
            parameters[8].Value = model.ParentPath;
            parameters[9].Value = model.ReadMe;
            parameters[10].Value = model.ClassDescription;
            parameters[11].Value = model.ClassKeywords;
            parameters[12].Value = model.AddDateTime;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(int ClassID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete K_ArticleClass ");
            strSql.Append(" where ClassID=@ClassID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ClassID", SqlDbType.Int,4)};
            parameters[0].Value = ClassID;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ModelArticleClass GetModel(int ClassID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 * from K_ArticleClass ");
            strSql.Append(" where ClassID=@ClassID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ClassID", SqlDbType.Int,4)};
            parameters[0].Value = ClassID;

            ModelArticleClass model = new ModelArticleClass();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ClassID"].ToString() != "")
                {
                    model.ClassID = int.Parse(ds.Tables[0].Rows[0]["ClassID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ChannelID"].ToString() != "")
                {
                    model.ChannelID = int.Parse(ds.Tables[0].Rows[0]["ChannelID"].ToString());
                }
                model.ClassName = ds.Tables[0].Rows[0]["ClassName"].ToString();
                if (ds.Tables[0].Rows[0]["Child"].ToString() != "")
                {
                    model.Child = int.Parse(ds.Tables[0].Rows[0]["Child"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Depth"].ToString() != "")
                {
                    model.Depth = int.Parse(ds.Tables[0].Rows[0]["Depth"].ToString());
                }
                if (ds.Tables[0].Rows[0]["OrderID"].ToString() != "")
                {
                    model.OrderID = int.Parse(ds.Tables[0].Rows[0]["OrderID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ParentID"].ToString() != "")
                {
                    model.ParentID = int.Parse(ds.Tables[0].Rows[0]["ParentID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["RootID"].ToString() != "")
                {
                    model.RootID = int.Parse(ds.Tables[0].Rows[0]["RootID"].ToString());
                }
                model.ParentPath = ds.Tables[0].Rows[0]["ParentPath"].ToString();
                model.ReadMe = ds.Tables[0].Rows[0]["ReadMe"].ToString();
                model.ClassDescription = ds.Tables[0].Rows[0]["ClassDescription"].ToString();
                model.ClassKeywords = ds.Tables[0].Rows[0]["ClassKeywords"].ToString();
                if (ds.Tables[0].Rows[0]["AddDateTime"].ToString() != "")
                {
                    model.AddDateTime = DateTime.Parse(ds.Tables[0].Rows[0]["AddDateTime"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM K_ArticleClass ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        public DataSet GetList(string strTop, string strWhere, string strOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (strTop.Trim() != "")
            {
                strSql.Append(" Top " + strTop);
            }
            strSql.Append(" * ");
            strSql.Append(" FROM K_ArticleClass ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            if (strOrder.Trim() != "")
            {
                strSql.Append(" Order by " + strOrder);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "K_ArticleClass";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }

        #endregion  成员方法

        public int GetMaxId(string fileName, string tableName)
        {
            return DbHelperSQL.GetMaxID(fileName, tableName);
        }
        public int GetMaxId(string fileName, string tableName, string whereStr)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Max(" + fileName + ") From S_ArticleClass");
            if (whereStr.Trim() != "")
            {
                strSql.Append(" where " + whereStr);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj != null)
            {
                return int.Parse("" + obj);
            }
            return 0;
        }

        public int ExecuteSql(string sql)
        {
            return DbHelperSQL.ExecuteSql(sql);
        }
    }
}
